﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;
using com.lover.astd.common.model.enumer;

namespace com.lover.astd.common.logicexe.activities
{
    public class WorldArmyExe : ExeBase
    {
        public WorldArmyExe() { _name = ConfigStrings.S_WorldArmy; _readable = ConfigStrings.SR_WorldArmy; }
        public override long execute()
        {
            if (!_user.isActivityRunning(ActivityType.WorldArmy)) return next_hour();
            Dictionary<string, string> conf = getConfig();
            if (!conf.ContainsKey(ConfigStrings.enabled) || !conf[ConfigStrings.enabled].ToLower().Equals("true")) return an_hour_later();
            int refresh_type = 0;
            if (conf.ContainsKey(ConfigStrings.refresh)) Int32.TryParse(conf[ConfigStrings.refresh], out refresh_type);
            bool buybox1 = conf.ContainsKey(ConfigStrings.buybox1) && conf[ConfigStrings.buybox1].ToLower().Equals("true");
            bool buybox2 = conf.ContainsKey(ConfigStrings.buybox2) && conf[ConfigStrings.buybox2].ToLower().Equals("true");
            ActivityMgr mgr = ServiceFactory.getActivityManager();

            int boxtype = 0;
            if (conf.ContainsKey(ConfigStrings.boxtype)) Int32.TryParse(conf[ConfigStrings.boxtype], out boxtype);

            int gold_available = getGoldAvailable();
            long cdMSeconds = 0;
            int result = mgr.handleWorldArmyInfo(_proto, _logger, ref gold_available, boxtype, buybox1, buybox2,
                refresh_type, out cdMSeconds);
            if (result == 2 || result == 10) return next_day();
            if (result == 1) return next_hour();
            if (cdMSeconds > 0) return cdMSeconds;
            return immediate();
        }
    }
}
